<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Article extends Controller
{
    protected $table_name = 'article';

    public function list(Request $request) {
        $article_list = DB::table($this->table_name)->orderBy('created_at', 'desc')->get();

        return view('article.article_list', ['article_list' => $article_list]);
    }
    public function create(Request $request) {
    	//var_dump($_POST);

    	$content = $request->input('content', '');
    	$author = $request->input('author', 'tonny');
        $title = $request->input('title', '');
        $created = $updated = date('Y-m-d H:i:s', time());
        $insert_data = [
            'title' => $title,
            'author' => $author,
            'content' => $content,
            'created_at' => $created,
            'updated_at'=> $updated
        ];

    	$res = DB::table($this->table_name)->insert($insert_data);
    	if ($res) {
//            return response()->json([
//                'message' => 'success post!'
//            ]);
            return redirect()->action(
                'Article@list'
            );
        }

    }
}
